package com.atguigu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName ListValueConstraintValidator
 * @Description TODO
 * @Author Administrator
 * @Date 2021/12/12 0012 23:30
 * @Version 1.0
 **/
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set = new HashSet<>();



    @Override
    public void initialize(ListValue listValue) {
        int[] vals = listValue.vals();
        for (int val : vals){
            set.add(val);
        }
    }

    /**
     *
     * @param integer 需要校验的值
     * @param constraintValidatorContext 校验结果上下文信息
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {


        return set.contains(integer);
    }
}
